{
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
}

unit ModifierEditors;

interface

uses
  Classes, SysUtils, VisualUtils, TestCore, MiscUtils;

type
  TModifierEditorRegistry = class
  private type
    TEntry = class
    private
      FModifierEditorClass: TFrameClass;
      FModifierClass: TModifierClass;
    public
      constructor Create(ModifierEditorClass: TFrameClass; ModifierClass: TModifierClass);
    end;
    TEntryList = TGenericObjectList<TEntry>;
  private
    FEntries: TEntryList;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Add(ModifierEditorClass: TFrameClass; ModifierClass: TModifierClass);
    function TryFind(ModifierClass: TModifierClass): TFrameClass;
  end;

function ModifierEditorRegistry: TModifierEditorRegistry;

implementation

var
  Registry: TModifierEditorRegistry;

function ModifierEditorRegistry: TModifierEditorRegistry;
begin
  Result := Registry;
end;

{ TModifierEditorRegistry.TEntry }

constructor TModifierEditorRegistry.TEntry.Create(
  ModifierEditorClass: TFrameClass; ModifierClass: TModifierClass);
begin
  inherited Create;
  FModifierEditorClass := ModifierEditorClass;
  FModifierClass := ModifierClass;
end;

{ TModifierEditorRegistry }

constructor TModifierEditorRegistry.Create;
begin
  inherited;
  FEntries := TEntryList.Create;
end;

destructor TModifierEditorRegistry.Destroy;
begin
  FreeAndNil(FEntries);
  inherited;
end;

procedure TModifierEditorRegistry.Add(ModifierEditorClass: TFrameClass;
  ModifierClass: TModifierClass);
begin
  FEntries.AddSafely(TEntry.Create(ModifierEditorClass, ModifierClass));
end;

function TModifierEditorRegistry.TryFind(ModifierClass: TModifierClass): TFrameClass;
var
  e: TEntry;
begin
  Result := nil;
  for e in FEntries do
    if e.FModifierClass = ModifierClass then
    begin
      Result := e.FModifierEditorClass;
      Break;
    end;
end;

initialization

  Registry := TModifierEditorRegistry.Create;

finalization

  FreeAndNil(Registry);

end.

